if(typeof tok != 'object')
{
	var tok = {};
}
/**
* @class
*/
tok.form = Class.create
(/**@lends tok.form*/{
	
	path: 'tbx_path',
	repos: 'sel_repos',
	user: 'sel_user',
	d1: 'tbx_d1',
	d2: 'tbx_d2',
	between: 'rdo_between',
	hours: 'tbx_hours',
	hour: 'rdo_hour',
	repo: 'div_repo',
	repo_b: 'div_repo_b',
	resCss: 'resource',
	cur_p: '',

	/**
	* extend class, set up event listeners and lookup current path in repo browser
	* @param	{object} params extend class parameters
	*/
	initialize: function(params)
	{
		Object.extend(this,params);
		Event.observe
		(
			this.path,
			'keydown',
			this.lookup.bind(this)
		);

		jQuery('#' + this.d1).datepicker({dateFormat: 'd M yy'});
		jQuery('#' + this.d2).datepicker({dateFormat: 'd M yy'});
		Event.observe(this.d1,'focus',this.check.curry(this.between));
		Event.observe(this.d2,'focus',this.check.curry(this.between));
		Event.observe(this.hours,'focus',this.check.curry(this.hour));
		Event.observe(this.repos,'change',this.get_authors.bind(this));
		$(this.path).focus();
		this.do_lookup();
	},

	/**
	* @private
	* check/tick given element
	*/
	check: function(el)
	{
		$(el).checked = true;
	},

	/**
	* tabbed-completion lookup
	* @private
	* @param	{object} e event
	*/
	lookup: function(e)
	{
		if(e.keyCode != Event.KEY_TAB)
			return;
		Event.stop(e);
		this.do_lookup();
	},

	/**
	* @private
	* lookup in repo browser based on contents of path input
	*/
	do_lookup: function()
	{
		new Ajax.Request
		(
			'/lookup.php',
			{
				method: 'post',
				parameters: 'path=' + $F(this.path) + '&repos=' + $F(this.repos),
				onComplete: this.write_path.bind(this)
			}
		);	
	},

	/**
	* @private
	* lookup in repo browser based on current path and file
	* @param	{string} file
	*/
	dir_lookup: function(file)
	{
		if(file == '../')
		{
			this.cur_p = this.cur_p.replace(/[^\/]*\/$/,'');
			file = '';
		}
		new Ajax.Request
		(
			'/dir.php',
			{
				method: 'post',
				parameters: 'path=' + this.cur_p + file + '&repos=' + $F(this.repos),
				onComplete: this.write_path.bind(this)
			}
		);	
	},

	/**
	* @private
	* return authors for selected repository
	*/
	get_authors: function()
	{
		new Ajax.Request
		(
			'/lookup_authors.php',
			{
				method: 'post',
				parameters: 'repos=' + $F(this.repos),
				onComplete: this.write_authors.bind(this)
			}
		);	
		

	},

	/**
	* @private
	* populate authors selection list
	* @param	{object} request ajax response
	*/
	write_authors: function(request)
	{
		if(request.responseText)
		{
			$(this.user).innerHTML = '';
			$(this.path).setValue('');
			this.do_lookup();
			var json = request.responseText.evalJSON();
			var o = document.createElement('option');
			o.setAttribute('value',0);
			o.setAttribute('selected','selected');
			o.innerHTML = '**ALL**';
			$(this.user).appendChild(o);
			var i = 0;
			var num = json.length;
			while(i < num)
			{
				var o = document.createElement('option');
				o.setAttribute('value',json[i]['id']);
				o.innerHTML = json[i]['user'];
				$(this.user).appendChild(o);
				++i;
			}
		}
	},

	/**
	* @private
	* write file to path input and file listing to repo browser
	* @param	{object} request ajax response
	*/
	write_path: function(request)
	{
		if(request.responseText)
		{
			var json = request.responseText.evalJSON();
			if(json[0])
			{
				jQuery('#' + this.repo).show();
				$(this.repo_b).innerHTML = '';
				var i = 0;
				var num = json[0].length;
				var sp = document.createElement('span');
				Element.addClassName(sp,this.resCss);
				Event.observe(sp,'click',this.click_path.curry(this));
				if(json[2])
				{
					sp.innerHTML = '../';
				}
				var el = document.createElement('div');
				el.appendChild(sp);
				$(this.repo_b).appendChild(el);
				while(i < num)
				{
					var sp = document.createElement('span');
					Element.addClassName(sp,this.resCss);
					Event.observe(sp,'click',this.click_path.curry(this));
					sp.innerHTML = json[0][i];
					var el = document.createElement('div');
					el.appendChild(sp);
					$(this.repo_b).appendChild(el);
					++i;
				}
			}
			if(json[1])
				$(this.path).setValue(json[1]);
			this.cur_p = json[2];
			$(this.path).focus();
		}
	},

	/**
	* @private
	* looks up directory or sets path to selected file
	* @param	{object} that value of class this
	*/
	click_path: function(that)
	{
		if(this.innerHTML.match(/\/$/))
			that.dir_lookup(this.innerHTML);
		else
			$(that.path).setValue(that.cur_p + this.innerHTML);
	},

	/**
	* @private
	* return directory name with subsequent file portion removed
	*/
	dirname: function(s)
	{
		return s.replace(/\\/g,'/').replace(/\/[^\/]*$/, '');
	}
});
